package cn.msqweb.src.controller;

import cn.msqweb.entity.Order;
import cn.msqweb.entity.ProdComm;
import cn.msqweb.model.Result;
import cn.msqweb.src.service.OrderService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import org.aspectj.weaver.ast.Or;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Api("系统订单业务接口管理")
@RequestMapping("order/order")
@RestController
public class SysOrderController {

    @Resource
    private OrderService orderService;

    @ApiOperation("多条件分页查询订单列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('order:order:page')")
    public Result<Page<Order>> loadSysOrderPage(@ApiParam("页码") @RequestParam Long current,
                                                @ApiParam("每页显示条数") @RequestParam Long size,
                                                @ApiParam("订单编号") @RequestParam(required = false) String orderNumber,
                                                @ApiParam("订单状态") @RequestParam(required = false) Integer status,
                                                @ApiParam("订单开始时间") @RequestParam(required = false) Date startTime,
                                                @ApiParam("订单结束时间") @RequestParam(required = false) Date endTime){

        Page<Order> page = orderService.querySysOrderPage(current,size,orderNumber,status,startTime,endTime);


        return Result.success(null);
    }

    @ApiOperation("根据订单编号查询系统单个订单详情")
    @GetMapping("orderInfo/{orderNumber}")
    @PreAuthorize("hasAuthority('order:order:info')")
    public Result<Order> loadSysOrderInfo(@ApiParam("订单编号") @PathVariable String orderNumber) {
        Order order = orderService.querySysOrderInfoByOrderNumber(orderNumber);
        return Result.success(order);
    }

    @ApiOperation("导出订单销售记录")
    @GetMapping("soldExcel")
    @PreAuthorize("hasAuthority('order:order:soldExcel')")
    public Result<String> exportSoldOrderExcel() {
        // 准备数据
        List<Order> list = orderService.list();
        String fileName = "E:\\" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(fileName, Order.class).sheet("模板111").doWrite(list);
        return Result.success(null);
    }
}
